/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import service.Employee;
import service.ManagementService_Service;
import util.Validator;

/**
 *
 * @author Tien
 */
@ManagedBean
@RequestScoped
public class EmployeeDetailBean {

    private String id;
    private String name;
    private String address;
    private String birthday;
    private String salary;
    private String error;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    private ManagementService_Service service;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    /**
     * Creates a new instance of EmployeeDetailBean
     */
    public EmployeeDetailBean() {
        service = new ManagementService_Service();
        request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        session = request.getSession();
        String empId = request.getParameter("id");
        if (empId != null && !empId.equals("") && Validator.isInteger(empId)) {
            Employee emp = service.getManagementServicePort().searchById(Integer.parseInt(empId));
            if (emp != null) {
                id = empId;
                address = emp.getAddrss();
                birthday = emp.getBirthday();
                name = emp.getName();
                salary = emp.getSalary() + "";
            } else {
                try {
                    response.sendRedirect("index.jsf");
                } catch (IOException ex) {
                    Logger.getLogger(EmployeeDetailBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } 
    }

    public void update() {
        if (!name.equals("") && !address.equals("") && !birthday.equals("") && !salary.equals("")) {
            if (Validator.isPositiveNumber(salary) && Validator.isDate(birthday)) {
                if (service.getManagementServicePort().update(Integer.parseInt(id), name, birthday, address, Double.parseDouble(salary))) {
                    try {
                        response.sendRedirect("index.jsf");
                    } catch (IOException ex) {
                        Logger.getLogger(EmployeeDetailBean.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {
                error = "Invalid information";
            }
        } else {
            error = "All information is required!";
        }
    }

    public void delete() {
        if (id != null) {
            if (service.getManagementServicePort().delete(Integer.parseInt(id))) {
                try {
                    response.sendRedirect("index.jsf");
                } catch (IOException ex) {
                    Logger.getLogger(EmployeeDetailBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                error = "Could not delete!";
            }
        }
    }
}
